﻿using ExpressionPlotterControl;
using Bestcode.MathParser;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tubes4_Metnum
{
    public partial class ResultForm : Form
    {
        private Point prevMouseCoord;
        private Point currMouseCoord;
        static String default_hasil_text = "Hasil";

        public ResultForm()
        {
            InitializeComponent();
            
            prevMouseCoord = currMouseCoord = new Point();
        }

        public void ShowHasil(double hasil)
        {
            ShowHasil(default_hasil_text, hasil);
        }

        public void ShowHasil(String hasiltext, double hasil)
        {
            Hasil1Label.Text = (String.IsNullOrEmpty(hasiltext)) ? default_hasil_text : hasiltext;
            Hasil1Text.Text = hasil.ToString();
            Hasil2Label.Visible = false;
            Hasil2Text.Visible = false;
        }

        public void ShowHasil(double hasil1, double hasil2)
        {
            ShowHasil(default_hasil_text, default_hasil_text, hasil1, hasil2);
        }

        public void ShowHasil(String hasil1text, String hasil2text, double hasil1, double hasil2)
        {
            Hasil1Label.Text = (String.IsNullOrEmpty(hasil1text)) ? default_hasil_text : hasil1text;
            Hasil1Text.Text = hasil1.ToString();
            Hasil2Label.Text = (String.IsNullOrEmpty(hasil2text)) ? default_hasil_text : hasil2text;
            Hasil2Text.Text = hasil2.ToString();
            Hasil2Label.Visible = true;
            Hasil2Text.Visible = true;
        }

        public void SetupResultInfo(String soal, String metode)
        {
            SoalText.Text = soal;
            MetodeText.Text = metode;
        }

        public void SetupResultLabel(double a, double b, int n)
        {
            aLabel.Text = a.ToString();
            bLabel.Text = b.ToString();
            hLabel.Text = ((b - a) / n).ToString();
            nLabel.Text = n.ToString();
        }

        // Belum kepake
        public void SetupResultLabel(double[] a, double[] b, int[] n)
        {
        }

        // ubah 
        public void SetupGraph(double[] x, double[] y, Expression[] fx_array)
        {
            expPlotter.RemoveAllExpressions();
            if (x != null)
            {
                if (y != null && fx_array != null)
                {
                    expPlotter.SetRangeX(x[0], x[1]);
                    expPlotter.SetRangeY(y[0], y[1]);
                    expPlotter.DivisionsX = 5; expPlotter.DivisionsY = 5;
                    expPlotter.PenWidth = 2;

                    foreach (Expression fx in fx_array)
                    {
                        expPlotter.AddExpression((IEvaluatable)fx, Color.Green, true);
                    }
                    expPlotter.Refresh();
                    setHandlers();
                }
            }            
        }

        public void RemoveGraph()
        {
            expPlotter.RemoveAllExpressions();
            expPlotter.Refresh();
        }

        public void SetupDataGrid(String[] columnHeader, String[] columnName, double[][] data, bool swap)
        {
            DataGrid.Rows.Clear();
            DataGrid.Columns.Clear();

            DataGrid.ColumnCount = (columnHeader.Length <= columnName.Length) ? columnHeader.Length : columnName.Length;
            for (int i = 0; i < DataGrid.ColumnCount; i++)
            {
                DataGrid.Columns[i].HeaderText = columnHeader[i];
                DataGrid.Columns[i].Name = columnName[i];
            }

            DataGrid.Rows.Add(data[0].Length);

            if (swap)
            {
                for (int i = 0; i < DataGrid.ColumnCount; i++)
                {
                    for (int j = 0; j < data[0].Length; j++)
                    {
                        DataGrid.Rows[j].Cells[i].Value = data[i][j];
                    }
                }
            }
            else
            {
                for (int i = 0; i < DataGrid.ColumnCount; i++)
                {
                    for (int j = 0; j < data[0].Length; j++)
                    {
                        DataGrid.Rows[j].Cells[i].Value = data[j][i];
                    }
                }
            }
        }

        public void SetupPerbandinganAnalitikKelistrikan(String expression, double value)
        {
            AnalitikKelistrikanPanel.Visible = true;
            FungsiAnalitikKelistrikan.Text = expression; 
            HasilAnalitikKelistrikan.Text = value.ToString();
        }

        public void RemovePerbandinganAnalitikKelistrikan()
        {
            AnalitikKelistrikanPanel.Visible = false;
        }

        // Diganti dengan fungsi SetupDataGrid(String[] columnHeader, String[] columnName, double[][] data)
        //public void SetupDataGrid(MetodeEnum metode, double[] data1, double[] data2, double[] data3)
        //{
        //    DataGrid.Rows.Clear(); 
        //    DataGrid.Columns.Clear();
            
        //    if (metode == MetodeEnum.Trapesium || metode == MetodeEnum.Simpson1per3)
        //    {
        //        DataGrid.ColumnCount = 2;
        //        DataGrid.Columns[0].HeaderText = "X";
        //        DataGrid.Columns[0].Name = "ColumnX";
        //        DataGrid.Columns[1].HeaderText = "Y";
        //        DataGrid.Columns[1].Name = "ColumnY";

        //        for (int i = 0; i < data1.Length; i++)
        //        {
        //            DataGrid.Rows.Add(new String[] { data1[i].ToString(), data2[i].ToString() });
        //        }
        //    }
        //    else if (metode == MetodeEnum.TitikTengah || metode == MetodeEnum.Simpson3per8)
        //    {
        //        DataGrid.ColumnCount = 2;
        //        DataGrid.Columns[0].HeaderText = "X";
        //        DataGrid.Columns[0].Name = "ColumnX";
        //        DataGrid.Columns[1].HeaderText = "Y";
        //        DataGrid.Columns[1].Name = "ColumnY";

        //        for (int i = 0; i < data1.Length; i++)
        //        {
        //            DataGrid.Rows.Add(new String[] { data1[i].ToString(), data2[i].ToString() });
        //        }
        //    }
        //    else if (metode == MetodeEnum.Simpson1per3)
        //    {

        //    }
        //    else if (metode == MetodeEnum.Simpson3per8)
        //    {

        //    }
        //    else if (metode == MetodeEnum.GaussLegrende)
        //    {
        //        DataGrid.ColumnCount = 3;
        //        DataGrid.Columns[0].HeaderText = "X";
        //        DataGrid.Columns[0].Name = "ColumnX";
        //        DataGrid.Columns[1].HeaderText = "Y";
        //        DataGrid.Columns[1].Name = "ColumnY";
        //        DataGrid.Columns[2].HeaderText = "C * Y";
        //        DataGrid.Columns[2].Name = "ColumnCxY";

        //        for (int i = 0; i < data1.Length; i++)
        //        {
        //            DataGrid.Rows.Add(new String[] { data1[i].ToString(), data2[i].ToString(), data3[i].ToString() });
        //        }
        //    }
        //    else if (metode == MetodeEnum.Romberg)
        //    {

        //    }
        //}

        // ubah 
        private void expPlotter_MouseEnter(object sender, EventArgs e)
        {
            expPlotter.Select();
        }

        // ubah 
        private void setHandlers()
        {
            this.expPlotter.MouseWheel += new MouseEventHandler(expPlotter_OnMouseWheel);
        }

        // ubah 
        private void expPlotter_OnMouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
                expPlotter.ZoomIn();
            else if (e.Delta < 0)
                expPlotter.ZoomOut();
            expPlotter.Refresh();
        }

        // ubah 
        private void expPlotter_MouseDown(object sender, MouseEventArgs e)
        {
            prevMouseCoord = e.Location;
            Console.Out.WriteLine(e.X + "-" + e.Y);
        }

        // ubah 
        private void expPlotter_MouseUp(object sender, MouseEventArgs e)
        {
            currMouseCoord = e.Location;
            Console.Out.WriteLine(e.X + "-" + e.Y);
            int XDist, YDist;
            XDist = currMouseCoord.X - prevMouseCoord.X;
            YDist = currMouseCoord.Y - prevMouseCoord.Y;
            if (Math.Abs(XDist) > Math.Abs(YDist))
            {
                if (XDist != 0)
                {
                    if (XDist > 0) expPlotter.MoveLeft(1);
                    else expPlotter.MoveRight(1);
                    expPlotter.Refresh();
                }
            }
            else
            {
                if (YDist != 0)
                {
                    if (YDist > 0) expPlotter.MoveUp(1);
                    else expPlotter.MoveDown(1);
                    expPlotter.Refresh();
                }
            }
        }

    }
}
